<template>
  <div class="lin_admin_login">
    <div class="lin_admin_login_flex_between">
      <div class="lin_admin_login_left">
        <div>
          <h1>后台管理系统</h1>
          <span>
            基于Nuxt3、JavaScript、TypeScript、Element Plus、Vue Router、Pinia、Vite等开发的后台管理，支持服务器端渲染(SSR)，
            使用门槛很低，完全免费开源，可以免费用于商业项目和其他场景!
          </span>
        </div>
      </div>
      <div class="lin_admin_login_right login-container">
        <form @submit.prevent="handleLogin">
          <div class="form-group">
            <label for="email">Email</label>
            <el-input
                v-model="email"
                size="large"
                type="text"
            >
              <template #prefix>
                <el-icon><UserFilled /></el-icon>
              </template>
            </el-input>
          </div>
          <div class="form-group">
            <label for="password">Password</label>
            <el-input
                v-model="password"
                size="large"
                type="password"
            >
              <template #prefix>
                <el-icon><QuestionFilled /></el-icon>
              </template>
            </el-input>
          </div>
          <button type="submit">Sign In</button>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
definePageMeta({
  layout: 'admin',
  index: 2
})

import { ref } from 'vue';
import { UserFilled, QuestionFilled } from '@element-plus/icons-vue'
import {ElNotification} from "element-plus";
import {useAuth} from "~/utils/login/sign";
import { useRouter } from 'vue-router';

const email = ref();
const password = ref();
const isLogin = ref(true);
const {handleSubmit, toAdmin} = useAuth();
const router = useRouter();


const handleLogin = async () => {
  if (email.value && password.value) {
    if(email.value === "starbucket" && password.value === "starbucket") {
      await handleSubmit(email.value, password.value, isLogin.value).then( res => {
        if (toAdmin.value){
          router.push("/admin/home")
        }
      })
    }
    else {
      ElNotification({
        title: 'Login',
        message: '请输入管理员账号！',
        type: 'warning',
        duration: 2000
      });
      return
    }

  }
  else {
    ElNotification({
      title: 'Login',
      message: '邮箱或者密码为空,请重试！',
      type: 'warning',
      duration: 2000
    });
    return
  }
};
</script>

<style scoped lang="scss">
.lin_admin_login {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
}


.lin_admin_login_left,
.lin_admin_login_right {
  width: 400px;
  height: auto;

}
.lin_admin_login_left {
  padding: 30px 0;
  text-align: center;
}
.lin_admin_login_left h1 {
  padding: 20px 0;
}
.lin_admin_login_left span {
  line-height: 25px;
}

.lin_admin_login_left img {
  width: 400px;
}
.form-group {
  margin-bottom: 15px;
}

label {
  font-size: 0.75rem;
  color: #8B8E98;
  font-weight: 600;
  display: block;
}

input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  width: 100%;
  height: 40px;
  padding: 10px;
  background-color: var(--color-background);
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #0056b3;
}

.error {
  color: red;
  margin-top: 10px;
}
</style>
